package com.usion.springbootmysql.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usion.springbootmysql.dao.CollegeDao;
import com.usion.springbootmysql.pojo.ResultVo;
import com.usion.springbootmysql.pojo.SearchDto;
import com.usion.springbootmysql.pojo.TbCollege;
import com.usion.springbootmysql.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeDao, TbCollege> implements CollegeService {
    @Autowired
    private CollegeDao collegeDao;
    @Override
    public ResultVo saveCollege(TbCollege tbCollege) {
        //添加之前，需要判断方向是否存在，存在不允许添加
//        QueryWrapper<TbCollege> query=new QueryWrapper<TbCollege>();
//        query.eq("name",tbCollege.getName());
        LambdaQueryWrapper<TbCollege> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TbCollege::getName, tbCollege.getName());
        int count=collegeDao.selectCount(lambdaQueryWrapper);
        if (count>0) {
            //代表存在不允许添加
            return new ResultVo(201,"名称存在，不允许添加",null);
        }else {
            collegeDao.insert(tbCollege);
            return new ResultVo(200,"添加成功",null);
        }
    }

    @Override
    public List<TbCollege> search(int status) {
        LambdaQueryWrapper<TbCollege> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TbCollege::getStatus,status);
        List<TbCollege> list = collegeDao.selectList(lambdaQueryWrapper);
        return list;
    }

    @Override
    public List<TbCollege> searchvo(@RequestBody TbCollege tbCollege) {
        LambdaQueryWrapper<TbCollege> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(tbCollege.getName())){
            lambdaQueryWrapper.like(TbCollege::getName,tbCollege.getName());
        }
        if (tbCollege.getStatus()!=null){
            lambdaQueryWrapper.eq(TbCollege::getStatus,tbCollege.getStatus());
        }
        List<TbCollege> list = collegeDao.selectList(lambdaQueryWrapper);
        return list;
    }

    @Override
    public IPage<TbCollege> searchPage(SearchDto searchDto) {
        IPage<TbCollege> page=new Page<TbCollege>(searchDto.getPage(),searchDto.getSize());
        LambdaQueryWrapper<TbCollege> lambdaQueryWrapper=new LambdaQueryWrapper<TbCollege>();
        if (!StringUtils.isEmpty(searchDto.getNmae())){
            lambdaQueryWrapper.like(TbCollege::getName,searchDto.getNmae());
        }
        if (searchDto.getStatus()!=null){
            lambdaQueryWrapper.eq(TbCollege::getStatus,searchDto.getStatus());
        }
        IPage<TbCollege> list = collegeDao.selectPage(page, lambdaQueryWrapper);
        return list;
    }

}
